--	configuration
local addon, ns = ...
local cfg = ns.cfg
local LabelUI = ns.LabelUI
local oUF = ns.oUF
local _G = _G

local BAR_TEXTURE
local TEXT_FONT

local point = LabelUI.point
local SpawnAura = LabelUI.SpawnAura
local fontcolor = {1, 1, 1, 1}
local hpBarColor = {1, 0, 0, 1}
local ppBarColor = {165/255, 73/255, 23/255, 1}
local bgBarColor = {44/255, 44/255, 44/255, 1}
local ppBgColor = {29/255, 63/255, 72/255}
local fontSize
local textureBackground

local LoadConfig = function()
	TEXT_FONT = cfg.font
	BAR_TEXTURE = cfg.statusbar
	fontSize = cfg.frames.bossbars.fontsize	
	textureBackground = cfg.textureBackground3
end

local ALTERNATE_POWER_INDEX = ALTERNATE_POWER_INDEX

oUF.Tags.Events['BossBars:altpower'] = 'UNIT_POWER'
oUF.Tags.Methods['BossBars:altpower'] = function(unit)
	local cur = UnitPower(unit, ALTERNATE_POWER_INDEX)
	local max = UnitPowerMax(unit, ALTERNATE_POWER_INDEX)
	if(max > 0 and not UnitIsDeadOrGhost(unit)) then
		return ("%s%%"):format(math.floor(cur/max*100+.5))
	else
		return ''
	end
end

oUF.Tags.Events['BossBars:health'] = 'UNIT_HEALTH'
oUF.Tags.Methods['BossBars:health'] = function(unit)
		local m = UnitHealthMax(unit)
		if(m == 0) then
			return 0
		else
			return math.floor(UnitHealth(unit)/m*100+.5)
		end
end

------------------------------------------------------------------------------

-- generate the frames
local function CreateUnitFrame(self, unit)
	local BarWidth = 210
	local BarHeight = 18
	
	-- size the overall unit frame
	self:SetSize(BarWidth, BarHeight)

	------------------------
	-- health bar
	------------------------
	local hpBar = CreateFrame("StatusBar", nil, self)
	hpBar:SetPoint("TOPLEFT", self, "TOPLEFT", 1, -1)
	hpBar:SetSize(BarWidth - 2, BarHeight - 2)
	hpBar:SetStatusBarTexture(BAR_TEXTURE)
	hpBar:GetStatusBarTexture():SetHorizTile(true)
	hpBar:SetStatusBarColor(unpack(hpBarColor))	
	hpBar:SetFrameStrata("MEDIUM")
	
	local hpBG = hpBar:CreateTexture(nil, "BORDER")
	hpBG:SetAllPoints(hpBar)
	hpBG:SetSize(BarWidth - 2, BarHeight - 2)
	hpBG:SetTexture(BAR_TEXTURE)
	hpBG:SetVertexColor(unpack(bgBarColor))	
	
	local hpBorder = hpBar:CreateTexture(nil, "BACKGROUND")
	hpBorder:SetPoint("TOPLEFT", hpBar, "TOPLEFT", -1, 1)
	hpBorder:SetSize(BarWidth, BarHeight + 8)
	hpBorder:SetTexture(0, 0, 0, 0.5)
	
	-- > see "oUF/elements/health.lua" for health bar coloring options
	hpBar.frequentUpdates = true
	hpBar.Smooth = true
	hpBar.colorHealth = false
	self.Health = hpBar
	
	------------------------
	-- power bar
	------------------------
	local ppBar = CreateFrame("StatusBar", nil, self)
	ppBar:SetPoint("BOTTOMLEFT", hpBar, "BOTTOMLEFT", 0, -8)
	ppBar:SetSize(BarWidth - 3, 7)
	ppBar:SetStatusBarTexture(BAR_TEXTURE)	
	ppBar:GetStatusBarTexture():SetHorizTile(true)
	ppBar:SetStatusBarColor(unpack(ppBarColor))
	ppBar:SetFrameStrata("MEDIUM")
	
	local ppBG = ppBar:CreateTexture(nil, "BORDER")
	ppBG:SetAllPoints(ppBar)
	ppBG:SetSize(BarWidth - 3, 7)
	ppBG:SetTexture(BAR_TEXTURE)
	ppBG:SetVertexColor(unpack(ppBgColor))
	
	--local ppBorder = ppBar:CreateTexture(nil, "BACKGROUND")
	--ppBorder:SetPoint("TOPLEFT", ppBar, "TOPLEFT", -1, 1)
	--ppBorder:SetSize(BarWidth, BarHeight)
	--ppBorder:SetTexture(0, 0, 0,  0.5)
	
	-- > see "oUF/elements/power.lua" for power bar coloring options
	ppBar.frequentUpdates = true
	ppBar.colorPower = true
	ppBar.Smooth = true
	self.Power = ppBar 
	
	------------------------
	-- unit name text
	------------------------

	local Name = hpBar:CreateFontString(nil, "Overlay")
	Name:SetFont(TEXT_FONT, fontSize)
	Name:SetShadowOffset(0,-1)
	Name:SetWidth((BarWidth - 2)*0.75)
	Name:SetHeight(BarHeight)
	Name:SetTextColor(unpack(fontcolor))
	Name:SetJustifyH("LEFT")
	Name:SetPoint("LEFT", hpBar, "LEFT", 5, 0)
	
	-- > see "oUF/elements/tags.lua" for provided tags
	self:Tag(Name, "[name]")
	
	------------------------
	-- unit health text
	------------------------
	local hpText = hpBar:CreateFontString(nil, "Overlay")
	hpText:SetFont(TEXT_FONT, fontSize)
	hpText:SetShadowOffset(.8,-.8)
	hpText:SetTextColor(unpack(fontcolor))
	hpText:SetJustifyH("RIGHT")
	hpText:SetPoint("RIGHT", hpBar, "RIGHT", -4, 0)
	
	-- > "see oUF/elements/tags.lua" for provided tags
	self:Tag(hpText, "[BossBars:health]%")
	
	-- handle cast bar
	self.Castbar = CreateFrame('StatusBar', nil, self)
	self.Castbar:SetFrameStrata("HIGH")
	self.Castbar:SetPoint("LEFT", self, "LEFT", 1, 0)
	self.Castbar:SetWidth(BarWidth - 2 )
	self.Castbar:SetHeight(BarHeight - 2)
	self.Castbar:SetStatusBarTexture(BAR_TEXTURE)	
	self.Castbar:GetStatusBarTexture():SetHorizTile(true)
	self.Castbar:SetStatusBarColor(29/255, 63/255, 72/255, 1)	
	
	-- cast bar background
	self.Castbar.bg = self.Castbar:CreateTexture(nil, "BACKGROUND")
	self.Castbar.bg:SetAllPoints(self.Castbar)
	self.Castbar.bg:SetTexture(0, 0, 0, .8)

	-- cast bar spell name text
	self.Castbar.Text = self.Castbar:CreateFontString(nil, 'OVERLAY')
	self.Castbar.Text:SetPoint('LEFT', self.Castbar, 2, 0)
	self.Castbar.Text:SetSize((BarWidth-2)* (2/3), BarHeight - 2)
	self.Castbar.Text:SetFont(TEXT_FONT, fontSize)
	self.Castbar.Text:SetJustifyH("LEFT")
	self.Castbar.Text:SetShadowOffset(.8,-.8)
	self.Castbar.Text:SetTextColor(unpack(fontcolor))

	-- cast bar spell time text
	self.Castbar.Time = self.Castbar:CreateFontString(nil, 'OVERLAY')
	self.Castbar.Time:SetPoint('RIGHT', self.Castbar, -3, 0)
	self.Castbar.Time:SetSize((BarWidth-2.1) * (1/3), BarHeight - 2.1)
	self.Castbar.Time:SetFont(TEXT_FONT, fontSize)
	self.Castbar.Time:SetShadowOffset(.8,-.8)
	self.Castbar.Time:SetTextColor(unpack(fontcolor))
	self.Castbar.Time:SetJustifyH("RIGHT")
	self.Castbar.CustomTimeText = function(_, t)
		self.Castbar.Time:SetText(("%.1f/%.1f"):format(self.Castbar.castIsChanneled and t or self.Castbar.max - t, self.Castbar.max))
	end
	
	-- alt power bar
	local AltPowerBar = CreateFrame("StatusBar", nil, self)
	AltPowerBar:SetFrameLevel(self.Health:GetFrameLevel() + 1)
	AltPowerBar:SetSize(103, 13)
	AltPowerBar:SetStatusBarTexture(BAR_TEXTURE)
	AltPowerBar:GetStatusBarTexture():SetHorizTile(false)
	AltPowerBar:SetStatusBarColor(1, 0, 0)
	AltPowerBar:SetPoint("BOTTOMRIGHT", self, "TOPRIGHT", -1, 1)
	AltPowerBar.Smooth = true
	self.AltPowerBar = AltPowerBar
	
	local bgAltPowerBar = AltPowerBar:CreateTexture(nil, "BORDER")
	bgAltPowerBar:SetAllPoints(AltPowerBar)
	bgAltPowerBar:SetTexture(BAR_TEXTURE)
	bgAltPowerBar:SetVertexColor(unpack(bgBarColor))
	
	local borderAltPowerBar = AltPowerBar:CreateTexture(nil, "BACKGROUND")
	borderAltPowerBar:SetPoint("TOPLEFT", AltPowerBar, "TOPLEFT", -1, 1)
	borderAltPowerBar:SetSize(105, 15)
	borderAltPowerBar:SetTexture(0, 0, 0, 0.5)
	
	-- alt power bar text
	local pbText = AltPowerBar:CreateFontString(nil, "Overlay")
	pbText:SetFont(TEXT_FONT, 11)
	pbText:SetShadowOffset(.8,-.8)
	pbText:SetTextColor(unpack(fontcolor))
	pbText:SetJustifyH("RIGHT")
	pbText:SetPoint("RIGHT", AltPowerBar, "RIGHT", -4, 0)
	
	self:Tag(pbText, "[BossBars:altpower]")


	self.border = CreateFrame("Frame", nil, self)
	self.border:SetPoint("TOPLEFT", hpBorder, "TOPLEFT", -2.5, 2.5) 
	self.border:SetPoint("BOTTOMRIGHT", hpBorder, "BOTTOMRIGHT", 2.5, -2.5)  
	self.border:SetFrameStrata("BACKGROUND") 
	self.border:SetBackdrop(textureBackground)
	self.border:SetBackdropColor(0, 0, 0, 0)	
	self.border:SetBackdropBorderColor(0, 0, 0, 0)
	self.border:SetScript('OnUpdate', function()
		if UnitIsUnit(unit, "target") then
			self.border:SetBackdropBorderColor(0, 156/255, 90/255, 1)
		elseif UnitIsUnit(unit, "focus") then
			self.border:SetBackdropBorderColor(133/255, 0, 196/255, 1)
		else
			self.border:SetBackdropBorderColor(0, 0, 0, 0)
		end
	end)
	
	self.Auras = SpawnAura(self, "bossbars", "debufs")
	--[[
	-- create buff at left of unit if they are boss units
	local buffs = CreateFrame("Frame", nil, self)
	buffs:SetHeight(26)
	buffs:SetWidth(252)
	buffs:SetPoint("RIGHT", self, "LEFT", TukuiDB.Scale(-4), 0)
	buffs.size = 26
	buffs.num = 3
	buffs.spacing = 2
	buffs.initialAnchor = 'RIGHT'
	buffs["growth-x"] = "LEFT"
	buffs.PostCreateIcon = TukuiDB.PostCreateAura
	buffs.PostUpdateIcon = TukuiDB.PostUpdateAura
	self.Buffs = buffs
	]]
end

local function CreateTargetFrame(self, unit)
	local BarWidth = 105
	local BarHeight = 15	
	
	self:SetSize(BarWidth, BarHeight)
	
	local hpBar = CreateFrame("StatusBar", nil, self)
	hpBar:SetStatusBarTexture(BAR_TEXTURE)
	hpBar:GetStatusBarTexture():SetHorizTile(true)	
	hpBar:SetStatusBarColor(unpack(hpBarColor))
	hpBar:SetSize(BarWidth - 2, BarHeight - 2)
	hpBar:SetPoint("TOPLEFT", self, "TOPLEFT", 1, -1)
	
	local hpBG = hpBar:CreateTexture(nil, "BORDER")
	hpBG:SetAllPoints(hpBar)
	hpBG:SetSize(BarWidth - 2, BarHeight - 2.1)
	hpBG:SetTexture(BAR_TEXTURE)
	hpBG:SetVertexColor(unpack(bgBarColor))

	local hpBorder = hpBar:CreateTexture(nil, "BACKGROUND")
	hpBorder:SetPoint("TOPLEFT", hpBar, "TOPLEFT", -1, 1)
	hpBorder:SetSize(BarWidth, BarHeight)
	hpBorder:SetTexture(0, 0, 0, 0.5)
	
	-- > see "oUF/elements/health.lua" for health bar coloring options
	hpBar.frequentUpdates = true
	hpBar.colorHealth = false
	hpBar.Smooth = true
	self.Health = hpBar
	
	local Name = hpBar:CreateFontString(nil, "Overlay")
	Name:SetFont(TEXT_FONT, 11)
	Name:SetShadowOffset(.8,-.8)
	Name:SetWidth(BarWidth)
	Name:SetHeight(BarHeight)
	Name:SetTextColor(unpack(fontcolor))
	Name:SetJustifyH("LEFT")
	Name:SetPoint("LEFT", hpBar, "LEFT", 5, 0)
	
	-- > see "oUF/elements/tags.lua" for provided tags
	self:Tag(Name, "[name]")	
end

------------------------------------------------------------------------------

local function Init()

LoadConfig()

oUF:RegisterStyle("oUF_BossBars", CreateUnitFrame)
oUF:RegisterStyle("oUF_BossTargets", CreateTargetFrame)

oUF:SetActiveStyle("oUF_BossBars")
local boss1 = oUF:Spawn("boss1", "oUF_Boss1")
boss1:SetPoint(cfg.frames.bossbars.pos.a1, point(cfg.frames.bossbars.pos.af), cfg.frames.bossbars.pos.a2, cfg.frames.bossbars.pos.x, cfg.frames.bossbars.pos.y)
local grow = {}
if cfg.frames.bossbars.grow == 'UP' then
	grow.a1 = "BOTTOM"
	grow.a2 = "TOP"
	grow.y = 30
elseif cfg.frames.bossbars.grow == 'DOWN' then
	grow.a1 = "TOP"
	grow.a2 = "BOTTOM"
	grow.y = -30
else
	grow.a1 = "CENTER"
	grow.a2 = "CENTER"
	grow.y = 0
end

local boss2 = oUF:Spawn("boss2", "oUF_Boss2")
boss2:SetPoint(grow.a1, oUF_Boss1, grow.a2, 0, grow.y)

local boss3 = oUF:Spawn("boss3", "oUF_Boss3")
boss3:SetPoint(grow.a1, oUF_Boss2, grow.a2, 0, grow.y)

local boss4 = oUF:Spawn("boss4", "oUF_Boss4")
boss4:SetPoint(grow.a1, oUF_Boss3, grow.a2, 0, grow.y)

local boss5 = oUF:Spawn("boss5", "oUF_Boss5")
boss5:SetPoint(grow.a1, oUF_Boss4, grow.a2, 0, grow.y)

local boss6 = oUF:Spawn("boss6", "oUF_Boss6")
boss6:SetPoint(grow.a1, oUF_Boss5, grow.a2, 0, grow.y)

oUF:SetActiveStyle("oUF_BossTargets")
local boss1T = oUF:Spawn("boss1target", "oUF_Boss1T")
boss1T:SetPoint("BOTTOMLEFT", oUF_Boss1, "TOPLEFT", 0, 0)

local boss2T = oUF:Spawn("boss2target", "oUF_Boss2T")
boss2T:SetPoint("BOTTOMLEFT", oUF_Boss2, "TOPLEFT", 0, 0)

local boss3T = oUF:Spawn("boss3target", "oUF_Boss3T")
boss3T:SetPoint("BOTTOMLEFT", oUF_Boss3, "TOPLEFT", 0, 0)

local boss4T = oUF:Spawn("boss4target", "oUF_Boss4T")
boss4T:SetPoint("BOTTOMLEFT", oUF_Boss4, "TOPLEFT", 0, 0)

local boss5T = oUF:Spawn("boss5target", "oUF_Boss5T")
boss5T:SetPoint("BOTTOMLEFT", oUF_Boss5, "TOPLEFT", 0, 0)

local boss6T = oUF:Spawn("boss6target", "oUF_Boss6T")
boss6T:SetPoint("BOTTOMLEFT", oUF_Boss6, "TOPLEFT", 0, 0)
end
LabelUI:AddInit(Init, "BossFrames")